//
//  MXHomePageCell.m
//  MXUserSys
//
//  Created by 王启颖 on 2018/10/10.
//  Copyright © 2018年 王启颖. All rights reserved.
//

#import "MXHomePageCell.h"
//#import "MXTagCollectionView.h"
#import "MXAuthController.h"
#import "MXUserViewController.h"

@interface MXHomePageCell ()
@property (weak, nonatomic) IBOutlet UIImageView *userHeader_image;
@property (weak, nonatomic) IBOutlet UILabel *userNickname_lbl;
@property (weak, nonatomic) IBOutlet UIImageView *messagePic_image;
@property (weak, nonatomic) IBOutlet UILabel *messageInfo_lbl;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *messagePic_heightLayout;
@property (weak, nonatomic) IBOutlet UILabel *messageAgreeNum_lbl;
@property (weak, nonatomic) IBOutlet UILabel *messageCommentNum_lbl;
@property (weak, nonatomic) IBOutlet UIButton *messageAgree_btn;
@property (weak, nonatomic) IBOutlet UIButton *messageComment_btn;
@property (weak, nonatomic) IBOutlet UIView *messageTag_view;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *messageTag_heightLayout;
@property (weak, nonatomic) IBOutlet UIButton *morePic_Btn;
@property (weak, nonatomic) IBOutlet UILabel *date_lbl;

@property (strong, nonatomic) NSMutableDictionary *agree_dicParams;
@end

@implementation MXHomePageCell

-(void)setModel:(MXHomePageModel *)model{
    _model = model;

    User *user = model.user;
    Picture *picture =[Picture mj_objectWithKeyValues:model.picturesList[0]];
    [self.userHeader_image changeCornerRadius:self.userHeader_image.width / 2];
    [self.userHeader_image sd_setImageWithURL:[NSURL URLWithString:user.user_headimg] placeholderImage:[UIImage imageNamed:@"photoAndVideoDefault"]];
    self.userNickname_lbl.text = user.user_nickname;
    self.messageInfo_lbl.text = model.messages_info;
    self.messagePic_heightLayout.constant = [picture.picture_size caculateToPicHeight];
    [self.messagePic_image sd_setImageWithURL:[NSURL URLWithString:picture.picture_url] placeholderImage:[UIImage imageNamed:@"photoAndVideoDefault"]];
    self.messageAgreeNum_lbl.text = [NSString stringWithFormat:@"%ld赞",(long)model.messages_agreenum];
    self.messageCommentNum_lbl.text = [NSString stringWithFormat:@"%ld评论",(long)model.messages_commentnum];
    self.messageAgree_btn.selected = model.is_agree;

    if (model.picturesList.count > 1) {
        [self.morePic_Btn setHidden:NO];
        [self.morePic_Btn setTitle:[NSString stringWithFormat:@"%ld",model.picturesList.count] forState:UIControlStateNormal];
    }else{
        [self.morePic_Btn setHidden:YES];
    }
    
    self.date_lbl.text = [self distanceTimeWithBeforeTime:model.created_at/1000];

}

- (IBAction)userClick:(id)sender {
    MXUserViewController *userVc = [[MXUserViewController alloc]init];
    userVc.user = _model.user;
    [self.navigationController pushViewController:userVc animated:YES];
}

- (NSString *)distanceTimeWithBeforeTime:(double)beTime
{
    NSTimeInterval now = [[NSDate date]timeIntervalSince1970];
    double distanceTime = now - beTime;
    NSString * distanceStr;
    
    NSDate * beDate = [NSDate dateWithTimeIntervalSince1970:beTime];
    NSDateFormatter * df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"HH:mm"];
    NSString * timeStr = [df stringFromDate:beDate];
    
    [df setDateFormat:@"dd"];
    NSString * nowDay = [df stringFromDate:[NSDate date]];
    NSString * lastDay = [df stringFromDate:beDate];
    
    if (distanceTime < 60) {//小于一分钟
        distanceStr = @"刚刚";
    }
    else if (distanceTime <60*60) {//时间小于一个小时
        distanceStr = [NSString stringWithFormat:@"%ld分钟前",(long)distanceTime/60];
    }
    else if(distanceTime <24*60*60 && [nowDay integerValue] == [lastDay integerValue]){//时间小于一天
        distanceStr = [NSString stringWithFormat:@"今天 %@",timeStr];
    }
    else if(distanceTime<24*60*60*2 && [nowDay integerValue] != [lastDay integerValue]){
        
        if ([nowDay integerValue] - [lastDay integerValue] ==1 || ([lastDay integerValue] - [nowDay integerValue] > 10 && [nowDay integerValue] == 1)) {
            distanceStr = [NSString stringWithFormat:@"昨天 %@",timeStr];
        }
        else{
            [df setDateFormat:@"MM-dd HH:mm"];
            distanceStr = [df stringFromDate:beDate];
        }
        
    }
    else if(distanceTime <24*60*60*365){
        [df setDateFormat:@"MM-dd HH:mm"];
        distanceStr = [df stringFromDate:beDate];
    }
    else{
        [df setDateFormat:@"yyyy-MM-dd HH:mm"];
        distanceStr = [df stringFromDate:beDate];
    }
    return distanceStr;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (IBAction)agreeClick:(UIButton *)sender {
    [[MXAccount sharedMXAccount] LoadTokenFromSandbox];
    NSString *token = [MXAccount sharedMXAccount].token;
    if (![token length]) {
        MXAuthController *authVC = [[MXAuthController alloc]init];
        [self.navigationController pushViewController:authVC animated:true];
        return;
    }
    
    sender.selected = !sender.selected;
    [self messageAgreeRequset];
}

- (IBAction)commentClick:(id)sender {
    [[MXAccount sharedMXAccount] LoadTokenFromSandbox];
    NSString *token = [MXAccount sharedMXAccount].token;
    if (![token length]) {
        MXAuthController *authVC = [[MXAuthController alloc]init];
        [self.navigationController pushViewController:authVC animated:true];
        return;
    }
    
    self.commentsBlock(self.model);
}

- (NSMutableDictionary *)agree_dicParams {
    if (!_agree_dicParams) {
        _agree_dicParams = [NSMutableDictionary dictionary];
    }
    [self initagree_dicParams];
    return _agree_dicParams;
}
- (void)initagree_dicParams {
    _agree_dicParams[@"user_id"] = [MXUserMessageTool message].user_id;
    _agree_dicParams[@"messages_id"] = self.model.messages_id;
}


#pragma mark - 网络请求 -
-(void)messageAgreeRequset{
    [self.messageAgree_btn setEnabled:NO];
    [[MXHttpManagerClass HttpRequstManager] PostURL:messageAgree params:self.agree_dicParams isHearderparam:YES success:^(NSInteger code, id result) {
        [self.messageAgree_btn setEnabled:YES];
        if (code == 0) {
            NSLog(@"%@",result);
            self.model.is_agree = !self.model.is_agree;
            if (self.model.is_agree) {
                self.model.messages_agreenum = self.model.messages_agreenum + 1;
            }else{
                self.model.messages_agreenum = self.model.messages_agreenum - 1;
            }
            self.messageAgreeNum_lbl.text = [NSString stringWithFormat:@"%ld赞",(long)self.model.messages_agreenum];
            self.agreesBlock(self.model);
        }else{
            [SVProgressHUD showImage:NULL_IMAGE status:@"参数错误"];
        }
        
    } fail:^(NSError *error) {
        [self.messageAgree_btn setEnabled:YES];
    }];
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    
    MXHomePageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = NSbunleloadNibName(@"MXHomePageCell");
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    return cell;
}

@end
